<?php
session_start();
require '../nette/Nette/loader.php';
require '../db.php';
require '../localization/lang.php';
require '../core/core.sanitizer.php';
require '../core/core.security.php';
require '../core/core.time.php';
require '../visual/visual.tasks.php';
require '../core/core.dfs.php';
require '../visual/visual.dfs.php';
require '../core/core.jobs.php';
$i=1;
//echo $_GET['q'];

function jobcompleter($q) {
	//echo $q."\n";
	$NO_of_jobs=1;
	$pid=0;
	$job=new core_job;
	$jobs=explode(">",$q);
	$NO_of_jobs=sizeof($jobs);
	//echo strpos($q,">");

	if(strpos($q,">") === false OR strpos($q,">") == 0) {
	//if ($NO_of_jobs == 100) {
			
			$query="select id,users from tree where lower(name) LIKE lower('%".mysql_real_escape_string($q)."%') AND disabled != 1";
			$result=mysql_query($query);
			while ($row=mysql_fetch_array($result)) {
				$users=explode(",",$row['users']);
				if (in_array($_SESSION['login'], $users))	{
				$job->jid=$row['id'];
				echo $job->returnjobpath()."\n";
				//echo $jobs[0];	
					}	
				} 		
			} else { 
				$cjbs=$NO_of_jobs-1;
				
				/*$query="select id,users from tree where lower(name) = lower('".mysql_real_escape_string($jobs[0])."') AND disabled != 1 AND PID = ".$pid;			
					//echo $q;
			
					$result=mysql_query($query);
					$row=mysql_fetch_array($result);
					$users=explode(",",$row['users']);
					if (in_array($_SESSION['login'], $users))	{
						$pid=$row['id'];
						}
					$query="select id,users from tree where  name LIKE '%".mysql_real_escape_string($jobs[1])."%' AND disabled != 1 AND PID =".$pid;
					$result=mysql_query($query);
					while ($row=mysql_fetch_array($result)) {
					$users=explode(",",$row['users']);
					if (in_array($_SESSION['login'], $users))	{
						$job->jid=$row['id'];
						echo $job->returnjobpath()."\n";	
						}
						}*/		
				for ($i = 0; $i < $cjbs; $i++) {				
					$query="select id,users from tree where lower(name) = lower('".mysql_real_escape_string($jobs[$i])."') AND disabled != 1 AND PID = ".$pid;			
					$result=mysql_query($query);
					$row=mysql_fetch_array($result);
					$users=explode(",",$row['users']);
					if (in_array($_SESSION['login'], $users))	{
						$pid=$row['id'];
						}
						
					}
					$query="select id,users from tree where lower(name) LIKE lower('%".mysql_real_escape_string($jobs[$i])."%') AND disabled != 1 AND PID =".$pid;
					$result=mysql_query($query);
					while ($row=mysql_fetch_array($result)) {
					$users=explode(",",$row['users']);
					if (in_array($_SESSION['login'], $users))	{
						$job->jid=$row['id'];
						echo $job->returnjobpath()."\n";	
						}		
					} 						
				}
echo "\n".$cjbs;
}

function jobcompleter_simple($q) {
	$out="";
	$job=new core_job;
	$query="select id,users from tree where lower(name) LIKE lower('%".mysql_real_escape_string($q)."%') AND disabled != 1";
	$result=mysql_query($query);
	while ($row=mysql_fetch_array($result)) {
		$users=explode(",",$row['users']);
		if (in_array($_SESSION['login'], $users))	{
			$job->jid=$row['id'];
			$out=$out.$job->returnjobpath()."\n";	
			}	
		} 
	return $out;
	}

function jobcompleter_advanced($q) {
	$pid=0;
	$job=new core_job;
	$jobs=explode(">",$q);
	$NO_of_jobs=sizeof($jobs)-1;
	for ($i = 0; $i < $NO_of_jobs; $i++) {
		$query="select id,users from tree where lower(name) = lower('".mysql_real_escape_string($jobs[$i])."') AND disabled != 1 AND PID = ".$pid;
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		$users=explode(",",$row['users']);
		if (in_array($_SESSION['login'], $users))	{
			$pid=$row['id'];
			}
		}
	//echo $jobs[$i];
	$query="select id,users from tree where lower(name) LIKE lower('%".mysql_real_escape_string($jobs[$i])."%') AND disabled != 1 AND PID =".$pid;
					$result=mysql_query($query);
					while ($row=mysql_fetch_array($result)) {
					$users=explode(",",$row['users']);
					if (in_array($_SESSION['login'], $users))	{
						$job->jid=$row['id'];
						echo $job->returnjobpath()."\n";	
						}		
					} 	
	}


$login=new core_security;
if ($login->checklogin()=="OK") {

/*
$job=new core_job;
$query="select id,users from tree where name LIKE '%".mysql_real_escape_string($_GET['q'])."%' AND disabled != 1";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
	$users=explode(",",$row['users']);
	if (in_array($_SESSION['login'], $users))	{
		$job->jid=$row['id'];
		echo $job->returnjobpath()."\n";	
		}	
	} 
//echo $query;*/
$q=$_GET['q'];
//if(strpos($_GET['q'],">") === false) {jobcompleter_simple($q);$_GET['q']="XXX";}
if(preg_match('/^.*>.*$/', $q) != 0) {jobcompleter_advanced($q);}else{echo jobcompleter_simple($q);}

//jobcompleter_advanced($_GET['q']);
} 


?>
